package ca.badenhorst.ourtrition.service;

import android.app.IntentService;
import android.content.Intent;
import android.net.http.AndroidHttpClient;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import ca.badenhorst.ourtrition.R;
import ca.badenhorst.ourtrition.activity.FoodGroupActivity;
import ca.badenhorst.ourtrition.db.DatabaseHelper;


/**
 * Created by wade on 09/04/14.
 */
public class CopyDBService extends IntentService {
    private String copyDatabaseUrl;

    public CopyDBService() {
        super("copyDBService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        copyDatabaseUrl = getString(R.string.database_url);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        BufferedOutputStream bos = null;
        InputStream is = null;
        boolean success = false;
        File file = null;
        AndroidHttpClient client = null;
        try {
            client = AndroidHttpClient.newInstance("");
            HttpGet request = new HttpGet(copyDatabaseUrl);
            HttpResponse response = client.execute(request);
            is = response.getEntity().getContent();
            file = getDatabasePath(DatabaseHelper.DATABASE_NAME);
            if (!file.exists()) {
                new File(file.getParent()).mkdirs();
                file.createNewFile();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));

            int bytesRead = 0;
            int byteCount = 4096;
            byte[] data = new byte[byteCount];
            while ((bytesRead = is.read(data, 0, byteCount)) > -1) {
                bos.write(data, 0, bytesRead);
            }

            success = true;
        } catch (IOException ioex) {
            ioex.printStackTrace();
        } finally {
            if (client != null) {
                client.close();
            }
            if (bos != null) {
                try {
                    bos.flush();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!success) {
                if (file != null) {
                    if (file.exists()) {
                        file.delete(); //clean up as this file did not download completely
                    }
                }
            }

            // processing done here.
            Intent broadcastIntent = new Intent();
            broadcastIntent.putExtra(FoodGroupActivity.RESULT, success ? FoodGroupActivity.SUCCESS : FoodGroupActivity.FAILURE);
            broadcastIntent.setAction(FoodGroupActivity.DB_COPIED);
            sendBroadcast(broadcastIntent);
        }
    }
}
